// pages/companycurse/curselist/curselist.js
const App = getApp()
// let AJAX = require('../../../utils/util.js').AJAX
// let imgUrl = require('../../../utils/util.js').imgUrl
// let timeTranfer = require('../../../utils/util.js').timeTranfer
let { AJAX, imgUrl, timeTranfer } = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH:"",
    page:1,
    dataList:[],
    total:0,
     imgUrl: imgUrl,
    isRefreshing: false,
    isLoadingMoreData: false,
    hasNoData: false, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: App.globalData.navHeight
    })
    this.requestList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let vm = this
    if (this.data.total > 0) {
      this.setData({
        isRefreshing: true
      })
  
      if (this.data.total > this.data.dataList.length) {
        this.setData({
          page: vm.data.page + 1,
          isLoadingMoreData: true
        })
        this.requestList(true)

      } else {
        this.setData({
          isLoadingMoreData: false
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navBack(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  requestList(str){
    let vm =this
    AJAX("articles","GET",{
      page:vm.data.page
    }).then(res=>{
      let _data = []
      if(str){
        _data = vm.data.dataList
      }
      if (res.data.items != '' || res.data.items.length>0){
        res.data.items.forEach(function (item) {
          item.published_at = timeTranfer(item.published_at, 1)
          _data.push(item)
        })

        vm.setData({
          dataList: _data,
          total: res.header["X-Pagination-Total-Count"],
          hasNoData: false
        })
      } else{
        this.setData({
          dataList: res.data,
          total: res.header["X-Pagination-Total-Count"],
          isLoadingMoreData: false,
          hasNoData: true
        })
      }
     
    })
  },
  toInfo(e){
    let _id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/companycurse/companyinfo/companyinfo?id='+_id,
    })
  }
})